<?php
class User_Model_Department 
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new User_Model_DbTable_Department();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ud_id 
	*@return boolean
	*/
	public  function update($row,$ud_id){
	
		$where = $this->_table->getAdapter()->quoteInto('ud_id = ?', $ud_id);
 
		return $this->_table->update($row, $where);
		
	}
		
	/**
	*Delete One Row
	*@param int $ud_id 
	*@return boolean
	*/
	public function delete($ud_id){
	
		$where = $this->_table->getAdapter()->quoteInto('ud_id = ?', $ud_id);		 
 
		return $this->_table->delete($where);
		
	}	
	
	public function getList( $condition=array(),$order='ud_sort', $count=0, $offset=0){
	
		if (isset($condition["ud_supervisor_id"]) && ''!=$condition["ud_supervisor_id"]) {
			$where = $this->_table->getAdapter()->quoteInto(" ud_supervisor_id=?",$condition["ud_supervisor_id"]);
		}
		if (isset($condition["ud_name"]) && ''!=$condition["ud_name"]) {
			$where = $this->_table->getAdapter()->quoteInto(" ud_name like ?","%".$condition["ud_name"]."%");
		}		
		return $this->_table->fetchAll($where, $order, $count, $offset)->toArray();	
	}
	
    public function getByCondition($condition,$type='*',$orderby="ud_sort",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["ud_supervisor_id"]) && $condition["ud_supervisor_id"]!=''){
            $select->where(" ud_supervisor_id =?",$condition["ud_supervisor_id"]);
        }
		if(isset($condition["ud_name"]) && $condition["ud_name"]!=''){
            $select->where(" ud_name like ?","%".$condition["ud_name"]."%");
        }	
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();

		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}
	
	public function getSelect($order='ud_sort'){
		$where = $this->_table->getAdapter()->quoteInto("?",1);
		return $this->_table->fetchAll($where, $order);
	}	
	
	public function getCount($departmentName=''){
		$where = $this->_table->getAdapter()->quoteInto("?",1);	
    	if (isset($departmentName) && ''!=$departmentName) {
			$where.=$this->_table->getAdapter()->quoteInto(" And  ud_name=?",$departmentName);
    	}		
		return $this->_table->fetchAll($where);	
	}	

	
	/**
	*Get Rowset By Department Name
	*@param string $ud_id 部门id
	*@return Rowset
	*/
	public function getByName($departmentName){
	
		$select = $this->_table->select()->where('ud_name = ?', $departmentName);	 
		
		return $this->_table->fetchRow($select);
		
	}
	
	/**
	*Get Rowset By Department 
	*@param string $ud_id 部门id
	*@return Rowset
	*/
	public function getById($ud_id){
		$where = $this->_table->getAdapter()->quoteInto("ud_id = ?",$ud_id);	
		return $this->_table->fetchRow($where);
	}
	
	
}